<template>
  <div>
    <div class="perMissTool">
      <el-input
        size="mini"
        placeholder="请输入英文角色名称"
        v-model="role.name"
      >
        <template slot="prepend">ROLE_</template>
      </el-input>
      <el-input
        size="mini"
        placeholder="请输入中文角色名称"
        v-model="role.nameZh"
        class="magCls"
      ></el-input>
      <el-button
        size="mini"
        type="primary"
        icon="el-icon-plus"
        class="magCls"
        @click="addRole"
        >添加</el-button
      >
    </div>
    <div style="margin-top: 10px" size="mini">
      <el-collapse v-model="activeName" accordion @change="change">
        <el-collapse-item
          :title="r.nameZh"
          :name="r.id"
          v-for="(r, index) in roles"
          :key="index"
        >
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>可访问的资源</span>
              <el-button
                style="float: right; padding: 3px 0; color: red"
                type="text"
                icon="el-icon-delete"
                @click="deleteRole(r)"
              ></el-button>
            </div>
            <div>
              <el-tree
                ref="tree"
                :key="index"
                node-key="id"
                :default-checked-keys="selectedMenus"
                :data="allMenus"
                show-checkbox
                :props="defaultProps"
              ></el-tree>
              <div class="btnClass">
                <el-button size="mini" @click="cancelUpdate"
                  >取消修改</el-button
                >
                <el-button
                  size="mini"
                  type="primary"
                  @click="daUpdate(r.id, index)"
                  >确认修改</el-button
                >
              </div>
            </div>
          </el-card>
        </el-collapse-item>
      </el-collapse>
    </div>
  </div>
</template>

<script>
export default {
  components: {},
  data() {
    return {
      role: {
        name: "",
        nameZh: "",
      },
      roles: [],
      defaultProps: {
        children: "children",
        label: "name",
      },
      activeName: -1,
      selectedMenus: [],
      allMenus: [],
    };
  },
  methods: {
    deleteRole(r) {
      this.$confirm("此操作将永久删除【"+r.nameZh+"】权限, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
          this.deleteRequset("/system/basic/permiss/role/" + r.id).then((resp) => {
            this.initRoles();
          });
        }).catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    addRole() {
      if (this.role.name && this.role.nameZh) {
        this.postRequset("/system/basic/permiss/role", this.role).then(
          (resp) => {
            if (resp) {
              this.role.name = "";
              this.role.nameZh = "";
              this.initRoles();
            }
          }
        );
      } else {
        this.$message.error("权限不能为空!");
      }
    },
    cancelUpdate() {
      this.activeName = -1;
    },
    daUpdate(rid, index) {
      let tree = this.$refs.tree[index];
      let selectKeys = tree.getCheckedKeys(true);
      let data = {
        rid: rid,
        mIds: selectKeys,
      };
      this.putRequset("/system/basic/permiss/", data).then((resp) => {
        this.activeName = -1;
      });
    },
    change(rid) {
      //参数为上面设置的name,这里给name赋值的id
      if (rid) {
        //判断是否选中
        this.initSelectedMenus(rid);
        this.intiAllMenus();
      }
    },
    intiAllMenus() {
      this.getRequset("/system/basic/permiss/getMenusWithChildren").then(
        (resp) => {
          if (resp) {
            this.allMenus = resp;
          }
        }
      );
    },
    initSelectedMenus(rid) {
      this.getRequset("/system/basic/permiss/mids/" + rid).then((resp) => {
        this.selectedMenus = resp;
      });
    },
    initRoles() {
      this.getRequset("/system/basic/permiss/").then((resp) => {
        if (resp) {
          this.roles = resp;
        }
      });
    },
  },
  mounted() {
    this.initRoles();
  },
};
</script>
<style scoped>
.perMissTool {
  /* 弹性盒子布局 */
  display: flex;
  /* 默认左边对齐 */
  justify-content: flex-start;
}
.perMissTool .el-input {
  width: 300px;
}
.magCls {
  margin-left: 5px;
}
>>> .el-collapse-item__header {
  height: 40px !important;
}
.btnClass {
  display: flex;
  justify-content: flex-end;
}
</style>